package panda.cardutil.other;

import java.util.List;
import java.util.ArrayList;

import panda.client.engine.MagicCard;
import panda.client.engine.MagicCardDB;
import panda.client.enumerations.MagicSet;

/**
 * This class generates boosters and tournament packs in text format.
 * TODO think about making a web interface to this.
 *
 * @author zane.tian@gmail.com
 */
public class TextProductGenerator
{
	private final static long appStartTime = System.currentTimeMillis();
	private final static boolean includesRules = false;

	private enum SealedType
	{
		/* 3 boosters, 3 boosters */
		NEW,

		/* 1 tournament pack, 3 boosters */
		OLD
	}

	public static void main(String[] args) throws Exception
	{
		System.out.println("Loading at " + (System.currentTimeMillis() - appStartTime) + "ms");

		generateSealedDeck(MagicSet.M_10, MagicSet.M_10, SealedType.NEW);
//		List<MagicCard> reborn = new ArrayList<MagicCard>();
//		reborn = MagicCardDB.get_AlaraReborn();
//		for(MagicCard card : reborn) {
//			System.out.println("1 " + card.getName());
//		}

		System.out.println("Done in: " + (System.currentTimeMillis() - appStartTime) + "ms");
	}

	/**
	 * Generates a old style Sealed Deck of: 1 tournament pack and 3 boosters or
	 * a new style Seatled deck of 3 boosters and 3 boosters.
	 *
	 * @param mainSet	  The main set.
	 * @param expansionSet The set of the booster packs.
	 */
	private static void generateSealedDeck(MagicSet mainSet, MagicSet expansionSet, SealedType type)
	{
		if (type == SealedType.OLD)
		{
			// System.out.println("Tournament Pack " + mainSet.getAbbrev() + " Contents:");
			List<MagicCard> tournamentPack = LimitedFactory.get().getPostShardsTournamentPack(mainSet);
			displayPackContents(tournamentPack);
		}
		else if (type == SealedType.NEW)
		{
			for (int i = 0; i < 3; i++)
			{
				// System.out.println("Booster Pack #" + (i + 1) + " " + mainSet.getAbbrev() + " Contents:");
				List<MagicCard> booster = LimitedFactory.get().getPostShardsBoosterPack(mainSet);
				displayPackContents(booster);
			}
		}

		for (int i = 0; i < 3; i++)
		{
			// System.out.println("Booster Pack #" + (i + 1) + " " + expansionSet.getAbbrev() + " Contents:");
			List<MagicCard> booster = LimitedFactory.get().getPostShardsBoosterPack(expansionSet);
			displayPackContents(booster);
		}
	}

	/**
	 * Displays a formatted representation of a pack of cards.
	 *
	 * @param cards The contents of the pack.
	 */
	private static void displayPackContents(List<MagicCard> cards)
	{
		for (MagicCard card : cards)
		{
			// System.out.print(card.getRarity().getRarityChar() + " ");

			/* casting cost is in format: 6WW (8) */
			// String costs[] = card.getCastingCost().split(" ");

			// System.out.print(pad(costs[0], 12) + " ");
			// System.out.print(pad(card.getName(), 30));
			System.out.print("1 " + card.getName());

			if (includesRules)
			{
				System.out.print(pad(card.getType(), 45));
				System.out.print(card.getBodyText());
			}

			System.out.println();
		}
		System.out.println("");
	}

	/**
	 * TODO Unfinished method. Outputs HTML to display cards in a webpage.
	 * @param cards The list of magiccards to be displayed.
	 */
	private static void generateHTMLImageGallery(List<MagicCard> cards)
	{
		for(MagicCard card : cards)
		{
			System.out.println("<img src='" + card.getUrl75() + "'/>");
		}
	}

	/**
	 * Pads a String with spaces up to a set length. Similar to C++'s setw
	 *
	 * @param input The String to be padded.
	 * @param width The width (in characters) to pad.
	 *
	 * @return A String with size "width" where any trailing characters are spaces.
	 */
	private static String pad(String input, int width)
	{
		int targetLength = input.length();
		if (width <= targetLength)
		{
			throw new IllegalArgumentException("Padding length " + width + " is not enough to pad target: " + input);
		}

		StringBuffer result = new StringBuffer();
		result.append(input);

		for (int i = 0; i < width - targetLength; i++)
		{
			result.append(" ");
		}

		return result.toString();
	}
}